<?php

namespace App\Http\Controllers\My\Pro;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Project;
use App\Model\Bid;
use App\Model\Att;
use Auth;
use DB;
use Gregwar\Captcha\CaptchaBuilder;

use App\Model\ChuanglanSMS;

class ProController extends Controller
{
    protected $middleware = ['App\Http\middleware\Authenticate'=>[]];
    public function jie(Request $r){
        $user = $r->user();
        $mobile = $user->mobile;

        

        return view('My.Pro.woyaojiekuan',['mobile'=>$mobile]);
    }

    public function captcha(){
        $builder = new CaptchaBuilder();
        $builder->build();
        header('Content-type: image/jpeg');
        $builder->output();
        $_SESSION['phrase'] = $builder->getPhrase();
    }

    public function postJie(Request $r){
        $pro = new Project();
        $att = new Att();
        $user = $r->user();

        $this->validate($r,
            [
            'age'=>'required|in:15,40,80',
            'money'=>'required|min:2',
            'mobile'=>'required|regex:/^1[34578]\d{9}$/'
            ],[
            'required'=>':attribute is required',
            ]);



        $pro->money = intval($r->money)*100;
        $pro->mobile = $r->mobile;
        $pro->uid = $user->uid;
        $pro->name = $user->name;
        $pro->pubtime = time();

        if(!$pro->save()){
            return Redirect::back();
        }

        $att->pid = $pro->pid;
        $att->uid = $user->uid;
        $att->age = $r->age;
        if(!$att->save()){
            return Redirect::back()->withInput()->with('人丑返回false');
        }else {
            return redirect('pro/prolist');
        }

    }

    public function prolist(){
        $user = Auth::user();
        $pro = Project::where('uid',$user->uid)->orderBy('pid','desc')->paginate(2);
        return view('My.Pro.prolist',['pro'=>$pro]);
    }

    public function check($id){
        $pro = Project::find($id);

        return view('My.Pro.shenhe',['pro'=>$pro]);

    }

    public function postCheck(Request $r,$id){
        $pro = Project::find($id);
        $att = Att::where('pid',$id)->first();

        $pro->title = $r->title;
        $pro->money = $r->money;
        $pro->hrange = $r->hrange;
        $pro->rate = $r->rate;
        $pro->status = $r->status;
        $att->realname = $r->realname;
        $att->gender = $r->gender;
        $att->udesc = $r->udesc;

        if( $pro->save()&&$att->save() ){
            return redirect('pro/prolist');
        }else {
            return Redirect::back()->withInput()->with('审核失败');
        }
    }

    public function mysy(){
        $user = Auth::user();
        $sy = DB::table('grows')->where('uid',$user->uid)->get();
        return view('My.User.mysy',compact('sy'));
    }

    public function myzd(){
        $user = Auth::user();
        $sy = DB::table('hks')->where('uid',$user->uid)->get();
        return view('My.Bid.myzd',compact('sy'));
    }

    public function mytz(){
        $user = Auth::user();
        $tz = Bid::where('bids.uid',$user->uid)->whereIn('projects.status',[1,2,3])->join('projects','projects.pid','=','bids.pid')->get(['bids.*','projects.status']);
        return view('My.Bid.mytz',compact('tz'));
    }

    public function massage($mobile){

        $rand = mt_rand(1000,9999);
        $massage = '【不二漫画】尊敬的用户，您好！欢迎注册不二漫画社。您的验证码是【'.$rand."】。\n详情请咨询:不二小杰\n咨询电话:18120785163";
        $sms=new ChuanglanSMS('N8217538','1cdfc8df');
        //发送短信
        $result=json_decode( $sms->send($mobile,$massage) );
        var_dump($result);
        if($result->success){
            session(['massage'=>$rand]);

        }
        echo session('massage');
    }

    public function massageCheck(Request $r){
        $rand = $session('massage');
        $builder = new CaptchaBuilder();
        $builder->build();

        if($r->verify === $rand){
            if($builder->testPhrase($r->imgcode)){
                echo 1;
            }
        }else {
            echo 0;
        }
    }



}
